
chrome.runtime.onMessage.addListener(async (msg, sender) => {

    console.log(msg)

    getPriceLimit(msg,20000);
})



function getPriceLimit(msg,price_mock){

    if(!msg.data.item.category_id){
         console.log('msg error');
        return;
    }

    var data={}
    data.company_id = "1458518";
    data.currency = "USD";
    data.marketplace_id = "1";
    data.old_price = "";
    data.price = price_mock+"";
    data.desc_category_id = msg.data.item.category_id;
    data.row_id = "1";
    var dataList=[];
    dataList.push(data);
    var dataRequest = {};
    dataRequest.data = dataList;
    dataRequest.locale="CN";
    console.log(dataRequest);
     $.ajax({
        url: 'https://seller.ozon.ru/api/pricing/price-validation/v3/validate-new',
　　　　 type: 'POST',
        data:JSON.stringify(dataRequest),
        processData: false,
        contentType: 'application/json',
        headers: {
            'X-O3-App-Name': 'seller-ui',
            'X-O3-Company-Id': '1458518',
            'X-O3-Language': 'zh-Hans',
            'X-O3-Page-Type': 'products-other'
          },
        xhrFields: {
            withCredentials: true // 这里设置了withCredentials
        },
        success: function(data) {

             if (data.error && data.error[0] && data.error[0].data && data.error[0].data[0].message.startsWith('请输入从')) {
                   var dataPro={};
                   dataPro.desc_category_id = msg.data.item.category_id;
                   dataPro.price_limit = data.error[0].data[0].value
                   $.post("https://www.gongrenbao.cn/index.php?c=api/ozon/updatelimit&type=price",
                      {dataPro:dataPro},
                      function(data){
                        console.log(data);
                      },
                  "text");
             }else if(price_mock > 20000 && data.error && data.error[0] && data.error[0].data && data.error[0].data[0].code=='price_too_big'){
                   //如果20000多了，设置一个2w刀
                   var dataPro={};
                   dataPro.desc_category_id = msg.data.item.category_id;
                   dataPro.price_limit = "20000;1;20000";
                   $.post("https://www.gongrenbao.cn/index.php?c=api/ozon/updatelimit&type=price",
                      {dataPro:dataPro},
                      function(data){
                        console.log(data);
                      },
                  "text");
             }else{
                getPriceLimit(msg,price_mock * 10);
             }
        },
        error: function(err) {
            console.error(err);
        }
    });
}


